package maman14;

/**
 * Each pixel consists of 3 colors (int):
 * Red,Green,Blue
 * Each color can hold up 0-255 values
 */
public class RGBColor {
	private int _red;
	private int _green;
	private int _blue;
	
	/**
	 * Default Constructor
	 * if no values passed then we create Black (0,0,0) 
	 */
	public RGBColor()
	{
		this(0,0,0);
	}
	
	/**
	 * Explicit Constructor
	 * We explicitly define our color by providing (R,G,B)
	 * if any of the values is out of range (0-255) then Default (black)  is created.
	 */
	public RGBColor(int red,int green,int blue)
	{
		// check if color out of range
		if (isColorValid(red) && isColorValid(blue) && isColorValid(green) )
		{
			_red = red;
			_green = green;
			_blue = blue;
		}
		else
		{
			//if value is out of range , use Default
			_red = 0;
			_green = 0;
			_blue = 0;
		}
	}
	
	/** 
	 * The Copy Constructor
	 */
	public RGBColor(RGBColor color)
	{
		this(color._red,color._green,color._blue);
	}
	
	
	// Getters
	public int getRed()
	{
		return _red;
	}
	
	public int getGreen()
	{
		return _green;
	}
	
	public int  getBlue()
	{
		return _blue;
	}
	
	//Setters
	public void setRed(int num)
	{
		if(isColorValid(num))
			_red = num;
	}
	
	public void setGreen(int num)
	{
		if(isColorValid(num))
			_green = num;
	}
	
	public void setBlue(int num)
	{
		if(isColorValid(num))
			_blue = num;
	}
	
	public String toString()
	{
		return "("+_red+","+_green+","+_blue+")";
	}
	
	/**
	 * Check if the current instance equals to other
	 * @param other
	 * @return if the colors are equals
	 */
	public boolean equals (RGBColor other)
	{
		if(other._red == _red &&
				other._green == _green &&
					other._blue == _blue )
			return true;
		
		return false;
		
	}
	
	/** returns the glayScale value of the class
	 * 30% red + 59% green + 11% blue
	 * @return
	 */
	public double convertToGrayScale()
	{
		
		double tempRed = 30*_red/100.0;
		
		double tempGreen = 59*_green/100.0;
		
		double tempBlue = 11*_blue/100.0;
		
		return (tempRed + tempGreen +tempBlue );
		
	}
	/** Inverts the color
	 * by completing it to the 255
	 * 255-X
	 */
	public void invert()
	{
		_red = 255 - _red;
		_green = 255 - _green;
		_blue = 255 - _blue;
	}
	
	
	/**
	 * mix the current color with other by:
	 * calculating AVG for each of the Elements
	 * @param other
	 */
	public void mix (RGBColor other)
	{
		_red = getAvg(_red,other._red);
		_green = getAvg(_green,other._green);
		_blue = getAvg(_blue,other._blue);

	}
	
	
	
	/**
	 * Check if the given base color is in valid Range (0-255)
	 */
	private boolean isColorValid (int intens)
	{
		if( intens>=0 && intens<=255)
			return true;
					
		return false;
		
	}
	

	
	
	/**
	 * Calculate avg  between two numbers, returns int
	 * @param num1
	 * @param num2
	 * @return
	 */
	private int getAvg (int num1,int num2)
	{
		return (int) ( (num1+num2)/2 );
	}
	
	
	
	 
	
	
	
	
}
